
import java.awt.List;
import java.beans.FeatureDescriptor;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FW {

	private static String pathToSWN = "G:\\study\\LVTN\\tool\\vn\\vn.hus.nlp.tagger-4.2.0-bin\\samples\\0.tagged.txt";

	// kiem tra co tu voi hay tu
	public static int countWith(String[] sentences) {
		int i = 0;
		for (String s : sentences) {
			if (s.equals("với/E")) {
				i++;
			}
		}
		return i;
	}

	// co bao nhieu từ tu
	public static int countENotWith(String[] sentences) {
		int i = 0;
		for (String s : sentences) {
			if (s.contains("/E") && s.equals("với/E") == false) {
				i++;
			}

		}
		return i;
	}

	// tim vi tri tu
	public static int searchE(String[] sentences) {
		int i = 0;
		for (int j = 0; j < sentences.length; j++) {
			if (sentences[j].equals("với/E") || sentences[j].contains("/E")) {
				i = j;
				break;
			}
		}
		return i;
	}

	//search vi tri tu
	public static ArrayList<Integer> getIndexListOfE(String[] sentences) {

		ArrayList<Integer> count_tu = new ArrayList<Integer>();
		for (int j = 0; j < sentences.length; j++) {
			if (sentences[j].contains("/E")) {
				count_tu.add(j);
			}

		}
		return count_tu;
	}

	// search N hay Np vi tri
	public static ArrayList<Integer> serach_NorNp(String[] sentences) {
		ArrayList<Integer> count_NorNp = new ArrayList<Integer>();
		for (int j = 0; j < sentences.length; j++) {
			if (NnotNc(sentences[j]) || sentences[j].contains("/Np")) {
				count_NorNp.add(j);
			}
		}
		return count_NorNp;
	}
	// N not Nc, Np
	public static boolean NnotNc(String word) {
		boolean out = false;
		if(word.contains("/Np"))
			out = false;
		else if(word.contains("/Nc"))
			out = false;
		else if(word.contains("/Nu"))
			out = false;
		else if(word.contains("/N"))
			out = true;
		return out;
	}
	// search N
	public static ArrayList<Integer> serach_N(String[] sentences) {
		ArrayList<Integer> count_N = new ArrayList<Integer>();
		for (int j = 0; j < sentences.length; j++) {
			if (NnotNc(sentences[j]) ) {
				count_N.add(j);
			}
		}
		return count_N;
	}
	// search A
	public static ArrayList<Integer> search_A(String[] sentences) {
		ArrayList<Integer> count_A = new ArrayList<Integer>();
		for (int j = 0; j < sentences.length; j++) {
			if (sentences[j].contains("/A")) {
				count_A.add(j);
			}
		}
		return count_A;
	}
	// search no , not
	public static int search_law2(String[] sentences) {
		int i = -1;
		for (int j = 0; j < sentences.length; j++) {
			if (sentences[j].equals("không/R") || sentences[j].equals("thiếu/R")) {
				i = j;
				break;
			}
		}
		return i;
	}

	// phu tro
	public static boolean check_a4_sub(String[] sentences, int a, int b) {
		boolean che = false;
		for (int i = a; i < b; i++) {
			if (sentences[i].contains("/V")) {
				che = true;
			}
		}
		return che;
	}
	
	public static void FW1(String[] sentences) {
		//System.out.println(countWith(sentences) +" "+ countENotWith(sentences));
		//có 1 từ with
		if (countWith(sentences) == 1 && countENotWith(sentences) == 0) {
			ArrayList<String> feature = new ArrayList<String>();
			int count_obj = 0;
			int indexOfE = searchE(sentences);

			for (int j = 0; j < indexOfE; j++) {
				String[] se_n = sentences[j].split("/");
				if (se_n[1].equals("Np") || NnotNc(se_n[1])) {
					count_obj++;
				}
			}
			for (int j = indexOfE + 1; j < sentences.length; j++) {
				String[] se_n = sentences[j].split("/");
				if (se_n[1].equals("Np") || NnotNc(se_n[1])) {
					feature.add(se_n[0]);
				}
			}
			if (feature.size() > 0 && count_obj > 0) {
				System.out.println(feature);
				//System.out.println("1a");
			}

		} // co 1 prepos
		else if (countENotWith(sentences) == 1 && countWith(sentences) == 0) {
			ArrayList<String> feature = new ArrayList<String>();
			int count_obj = 0;

			for (int j = 0; j < searchE(sentences); j++) {
				String[] se_n = sentences[j].split("/");
				if (se_n[1].equals("Np") || NnotNc(se_n[1])) {
					feature.add(se_n[0]);
				}
			}
			for (int j = searchE(sentences) + 1; j < sentences.length; j++) {
				String[] se_n = sentences[j].split("/");
				if (se_n[1].equals("Np") || NnotNc(se_n[1])) {
					count_obj++;
				}
			}
			if (feature.size() > 0 && count_obj > 0) {
				System.out.println(feature);
				//System.out.println("1b");
			}

		} // co ca 2
		else if (countENotWith(sentences) >= 1 || countWith(sentences) >= 1) {
			// co prepos không có với		
			if (countENotWith(sentences) > 1 && countWith(sentences) == 0) {
				ArrayList<Integer> count_tu = getIndexListOfE(sentences);
				ArrayList<String> feature = new ArrayList<String>();

				for (int i : count_tu) {
					//System.out.println(i);
					if(i == 0)
						continue;
					if ((NnotNc(sentences[i - 1]) || sentences[i - 1].contains("/Np")) && (NnotNc(sentences[i + 1]) || sentences[i + 1].contains("/Np"))) {
						String[] se_n = sentences[i - 1].split("/");
						feature.add(se_n[0]);
					}
				}
				if (feature.size() > 0) {
					System.out.println(feature);
					//System.out.println("1c1");
				}

			} // có với không có prepos
			else if (countENotWith(sentences) == 0 && countWith(sentences) > 1) {
				ArrayList<Integer> count_tu = getIndexListOfE(sentences);
				ArrayList<String> feature = new ArrayList<String>();

				for (int i : count_tu) {
					if(i == 0)
						continue;
					if ((NnotNc(sentences[i - 1]) || sentences[i - 1].contains("/Np")) && (NnotNc(sentences[i + 1]) || sentences[i + 1].contains("/Np"))) {
						String[] se_n = sentences[i + 1].split("/");
						feature.add(se_n[0]);
					}
				}
				if (feature.size() > 0) {
					System.out.println(feature);
					//System.out.println("1c2");
				}

			} // có cả hai
			else if (countENotWith(sentences) > 0 && countWith(sentences) > 0) {

				ArrayList<String> feature = new ArrayList<String>();
				ArrayList<Integer> count_tu = getIndexListOfE(sentences);
				for (int i : count_tu) {
					if(i == 0)
						continue;
					if (sentences[i].equals("với/E") && (NnotNc(sentences[i - 1]) || sentences[i - 1].contains("/Np")) && (NnotNc(sentences[i + 1]) || sentences[i + 1].contains("/Np"))) {
						String[] se_n = sentences[i + 1].split("/");
						feature.add(se_n[0]);
					} else if ((NnotNc(sentences[i - 1]) || sentences[i - 1].contains("/Np")) && (NnotNc(sentences[i + 1]) || sentences[i + 1].contains("/Np")))
					{
						String[] se_n = sentences[i - 1].split("/");
						feature.add(se_n[0]);
						System.out.println("e");
					}
					else;
				}
				if (feature.size() > 0) {
					System.out.println(feature);
					//System.out.println("1c3");
				}

			}
		}
		//System.out.println("1");
	}

	//check  N + O or O + N
	public static void FW2(String[] sentences) {
		
		//if (countWith(sentences) == 0 && countENotWith(sentences) == 0) 
		{
			ArrayList<String> feature = new ArrayList<String>();
			ArrayList<Integer> count_N = serach_N(sentences);
			for(int i = 0; i < count_N.size()-1; i++){
				if(NnotNc(sentences[count_N.get(i) + 1]) || sentences[count_N.get(i) + 1].contains("/Np"))
				{
					String[] se_n = sentences[count_N.get(i)].split("/");
					feature.add(se_n[0]);
				}
				//System.out.println(sentences[count_N.get(i)]);
			}

			if (feature.size() > 0 ) {
				System.out.println(feature);
				//System.out.println("2");
			}
		}
	}

	// O + V + N/NP.
	public static void FW3(String[] sentences) {
		//System.out.println(countWith(sentences) +" "+ countENotWith(sentences));
		ArrayList<String> feature = new ArrayList<String>();
		//if (countWith(sentences) == 0 && countENotWith(sentences) == 0)
		{
			ArrayList<Integer> count_NorNp = serach_NorNp(sentences);
			for (int i = 0; i < count_NorNp.size() - 1; i++) {
				if (check_a4_sub(sentences, count_NorNp.get(i), count_NorNp.get(i + 1))) {
					String[] se_n = sentences[count_NorNp.get(i + 1)].split("/");
					feature.add(se_n[0]);
				}
			}
		}
		if (feature.size() > 0) {
			System.out.println(feature);
			//System.out.println("3");
		}
	}

	// Second rule: “No” patterns.
	public static void FW4(String[] sentences) {
		ArrayList<String> feature = new ArrayList<String>();

		if (search_law2(sentences) >= 0) {
			for (int i = search_law2(sentences); i < sentences.length; i++) {
				String[] se_n = sentences[i].split("/");
				if (NnotNc(se_n[1]) || se_n[1].equals("Np")) {
					feature.add(se_n[0]);
				}
			}
		}
		if (feature.size() > 0) {
			System.out.println(feature);
			//System.out.println("4");
		}

	}
	// Add: N/Np/P + A
	public static void FW5(String[] sentences) {
		ArrayList<String> feature = new ArrayList<String>();
		ArrayList<Integer> Count_Adj = search_A(sentences);
		if(search_A(sentences).size() > 0){
			int j = 0;
			for(int i : Count_Adj){
				for(int k = j; k < i; k++){
					if(sentences[k].contains("/Np") || sentences[k].contains("/P") || NnotNc(sentences[k])){
						String[] fe_n = sentences[k].split("/");
						feature.add(fe_n[0]);
					}
				}
				j = i;
			}
		}
		if (feature.size() > 0) {
			System.out.println(feature);
			//System.out.println("4");
		}

	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			BufferedReader csv = new BufferedReader(new FileReader(pathToSWN));
			String line = "";
			try {
				while ((line = csv.readLine()) != null) {
					String[] data = line.split(" ");
					FW1(data);
					FW2(data);
					FW3(data);
					FW4(data);
					FW5(data);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
